﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;
using System.Collections;
namespace BLL
{
    public class NextOfKinBLL
    {
        HospitalDataContext DB = new HospitalDataContext();
        private static int nokid = 0;
        public List<NextOfKin> SelectAll()
        {
            return DB.NextOfKins.ToList();
        }
        public string Insert(NextOfKinDTO nhap)
        {
            try
            {
                NextOfKin nextok = new NextOfKin();
                nextok.NOKID = (nokid++).ToString();
                nextok.FullName = nhap.Fullname;
                nextok.Relationship = nhap.Relationship;
                nextok.Address = nhap.Address;
                nextok.Tel = nhap.Tel;

                DB.NextOfKins.InsertOnSubmit(nextok);
                DB.SubmitChanges();
                return "success";
            }
            catch (Exception e)
            {
               return e.Message;
            }
        }
        //-------ham update--------------
        public string Update(NextOfKinDTO nhap)
        {
            try
            {
                NextOfKin nextok = DB.NextOfKins.Where(nok => nok.NOKID==nhap.Nokid).FirstOrDefault();
                nextok.FullName = nhap.Fullname;
                nextok.Relationship = nhap.Relationship;
                nextok.Address = nhap.Address;
                nextok.Tel = nhap.Tel;

                
                DB.SubmitChanges();
                return "success";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
        //get ID
        public string GetID(NextOfKinDTO nhap)
        {
            //NextOfKin id = from nok in DB.NextOfKins
            //               where nok.FullName == nhap.Fullname && nok.Relationship == nhap.Relationship && nok.Address == nhap.Address && nok.Tel == nhap.Tel
            //               select nok;
            //return id.NOKID;
            string id = DB.NextOfKins.Where(nok => nok.FullName == nhap.Fullname && nok.Relationship == nhap.Relationship && nok.Address == nhap.Address && nok.Tel == nhap.Tel).Select(n => n.NOKID).FirstOrDefault().ToString();
            return id;
        }
        public string SelectID(string key)
        {
            return DB.NextOfKins.Where(nok => nok.FullName.Contains(key)).Select(trave => trave.NOKID).FirstOrDefault().ToString();
        }
        public List<NextOfKinAndPatientDTO> SearchWithPatientID(string key)
        {
            return DB.NextOfKins.Join(
                DB.Patients,
                a => a.NOKID,
                b => b.NOKID,
                (a, b) => new NextOfKinAndPatientDTO
                {
                    NOKID = a.NOKID,
                    FullName = a.FullName,
                    Relationship = a.Relationship,
                    Address = a.Address,
                    Tel = a.Tel,
                    PatientID = b.PatientID
                }
                ).Where(nok => nok.PatientID.Contains(key)).ToList < NextOfKinAndPatientDTO>();
        }
    }
}
